export default function ({ isServer, req, redirect, route }) {
  let isMobile = (ua) => {
    return !!ua.match(/AppleWebKit.*Mobile.*/)
  }
  if (!req) return
  let userAgent = req.headers["user-agent"]
  // http不能直接写死，要取出来，可能是https
  const request = req?.headers?.referer?.split("://")[0] || "https"
  //console.log("请求头信息", request);
  const isRouterMobile = route.fullPath.includes("m-")
  // 如果是移动端，但是确访问了pc的路径，重定向到移动端
  if (isMobile(userAgent) && !isRouterMobile) {
    console.log("重定向到移动端", req.headers.host, route.fullPath)
    return redirect(`${request}://${req.headers.host}` + `/m-${route.fullPath.substring(1, route.fullPath.length)}`)
  } else if (!isMobile(userAgent) && isRouterMobile) {
    // 如果是pc端，但是确访问了移动端的路径，重定向到pc端
    console.log("重定向到PC端", req.headers.host, route.fullPath)
    let isIncludes = route.fullPath.includes("m-products-list")
    return redirect(`${request}://${req.headers.host}` + (isIncludes ? "/products" : `/${route.fullPath.substring(3, route.fullPath.length)}`))
  }
}
